package org.ndot.java.util.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 【小蚂蚁学堂 - J2SE_Study】
 * 
 * 项目名称：J2SE_Study
 * 
 * 文件名：TestFixedThreadPool.java
 * 
 * 功 能:
 * 
 * 
 * 作 者: SunJincheng
 * 
 * 创建时间: 2010-4-8
 * 
 */
public class TestFixedThreadPool3 {
	public static void main(String[] args) {
		// 创建一个可根据需要创建新线程的线程池，但是在以前构造的线程可用时将重用它们。
		ExecutorService pool = Executors.newCachedThreadPool();

		// 创建实现了Runnable接口对象，Thread对象当然也实现了Runnable接口
		Thread t1 = new MyThread();
		Thread t2 = new MyThread();
		Thread t3 = new MyThread();
		Thread t4 = new MyThread();
		Thread t5 = new MyThread();
		// 将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		try {
			// 以便重复利用线程池中空闲的线程
			Thread.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		pool.execute(t5);
		// 关闭线程池
		pool.shutdown();
	}
}
